package com.maimang.platform.controller.system;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysDictData;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.common.service.SysDictDataService;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysDeptController
 * @描述 <br>数字字典信息</br>
 * @日期 2019-04-24
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/system/dict/data")
@Api(value = "数据字典", description = "系统通用数据字典信息操作")
public class SysDictDataController extends BaseController {
	private String prefix = "system/dict/data";

	@Autowired
	private SysDictDataService dictDataService;

	@RequiresPermissions("system:dict:view")
	@GetMapping()
	public String dictData() {
		return prefix + "/note";
	}

	@ApiOperation(value = "字典列表", notes = "数据字典列表信息")
	@PostMapping("/list")
	@RequiresPermissions("system:dict:list")
	@ResponseBody
	public TableDataInfo list(SysDictData dictData) {
		startPage();
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		return getDataTable(list);
	}

	@ApiOperation(value = "导出字典", notes = "导出数据字典信息列表到excel表格")
	@Log(title = "字典数据", businessType = BusinessType.EXPORT)
	@RequiresPermissions("system:dict:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(SysDictData dictData) {
		List<SysDictData> list = dictDataService.selectDictDataList(dictData);
		ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
		return util.exportExcel(list, "字典数据");
	}

	/**
	 * 新增字典类型
	 */
	@GetMapping("/add/{dictType}")
	public String add(@PathVariable("dictType") String dictType, ModelMap modelMap) {
		modelMap.put("dictType", dictType);
		return prefix + "/add";
	}

	/**
	 * 新增保存字典类型
	 */
	@Log(title = "字典数据", businessType = BusinessType.INSERT)
	@ApiOperation(value = "新增字典", notes = "新增保存字典类型信息")
	@RequiresPermissions("system:dict:add")
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysDictData dict) {
		dict.setCreateUser(ShiroUtils.getLoginName());
		return toResult(dictDataService.insertDictData(dict));
	}

	/**
	 * 修改字典类型
	 */
	@GetMapping("/edit/{dictCode}")
	public String edit(@PathVariable("dictCode") Long dictCode, ModelMap modelMap) {
		modelMap.put("dict", dictDataService.selectDictDataById(dictCode));
		return prefix + "/edit";
	}

	/**
	 * 修改保存字典类型
	 */
	@Log(title = "字典数据", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "修改字典", notes = "修改数据字典信息")
	@RequiresPermissions("system:dict:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysDictData dict) {
		dict.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(dictDataService.updateDictData(dict));
	}

	@Log(title = "字典数据", businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除字典", notes = "删除数据字典信息")
	@RequiresPermissions("system:dict:remove")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(dictDataService.deleteDictDataByIds(ids));
	}
}
